package com.dk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dk.mapper.PartanduserMapper;
import com.dk.pojo.Menu;
import com.dk.mapper.MenuMapper;
import com.dk.pojo.Menuandpart;
import com.dk.pojo.Partanduser;
import com.dk.pojo.Users;
import com.dk.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-07-08
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Autowired
    private MenuMapper mapper;
    @Autowired
    private PartanduserMapper paumapper;

    public List<Menu> resMenu(List<Menu> plist,  List<Menu> clist ){
        for (Menu menu : plist) {
            menu.setMenu(new ArrayList<Menu>());
            for (Menu menu1 : clist) {
                if (menu.getId()==menu1.getParentid())
                    menu.getMenu().add(menu1);
            }
            if (!CollectionUtils.isEmpty(menu.getMenu())){
                resMenu(menu.getMenu(),clist);
            }
        }
        return plist;
    }

    @Override
    public List<Menu> getMenu(Integer id) {
//        System.out.println(users.toString());

        List<Menu> list = mapper.getPower(id);
        List<Menu> plist = list.stream().distinct().filter((menu) -> menu.getParentid() == null).collect(Collectors.toList());
        List<Menu> cmap = list.stream().distinct().filter((menu) -> menu.getParentid() != null).collect(Collectors.toList());
        return resMenu(plist, cmap);

    }

    @Override
    public IPage<Menu> getMenuList(Page<Menu> page, Menu menu) {
        QueryWrapper<Menu> wrapper=new QueryWrapper<>();

        if (!StringUtils.isEmpty(menu.getName())){
            wrapper.like("name",menu.getName());
        }
        if (!StringUtils.isEmpty(menu.getStart())){
            wrapper.eq("start",menu.getStart());
        }
        if (!StringUtils.isEmpty(menu.getType())){
            wrapper.eq("type",menu.getType());
        }

//        usersMapper.select
        return mapper.selectPage(page,wrapper);
    }

    @Override
    public List<Menu> getPartList() {
        QueryWrapper<Menu> queryWrapper=new QueryWrapper<>();
        queryWrapper.ne("type","2");
        return mapper.selectList(queryWrapper);
    }

    @Override
    public List<Menu> getMenuAll() {
        List<Menu> list = mapper.selectList(new QueryWrapper<>());
        List<Menu> plist = list.stream().filter((menu) -> menu.getParentid() == null).collect(Collectors.toList());
        List<Menu> cmap = list.stream().filter((menu) -> menu.getParentid() != null).collect(Collectors.toList());
        return resMenu(plist, cmap);

    }

    @Override
    public List<Integer> getHaveMenu(Integer id) {
        return mapper.getpartPower(id);
    }

}
